<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2020-11-30 18:31:33
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-add">
        <el-dialog
        :title="$t('device-join-update-group')"
        :visible="visibleAdd"
        width="35%"
        @close="()=>{$store.commit('setVisibleAdd',false)}"
        :close-on-click-modal='false'
        >   
            <el-row :gutter="20">
                <el-col :span="19" :offset="2">
                    <el-form ref="form" :model="form" label-width="120px" >
                        <el-form-item :label="$t('device-group-name')" prop="upgradeGroupId" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.upgradeGroupId" filterable :placeholder="$t('pleaseSelect')" clearable >
                                <el-option
                                    v-for="item in groupListData"
                                    :key="item.sid"
                                    :label="item.groupName"
                                    :value="item.sid">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setVisibleAdd',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {
    },
    props: {
        selectDataList:{
            type:Array,
            default:()=>{return []}
        },
    },
    data() {
        return {
            form:{
                upgradeGroupId:""
            },
            cloudType:[{
                id:'0',
                text:'Solax Cloud'
            },{
                id:'1',
                text:'QCELLS'
            }],
            groupListData:[]
        };
    },
    computed: {
        visibleAdd(){
            return this.$store.state.sysStore.visibleAdd
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {
        this.upgradegroupGetGroupList()
    },
    methods: {
        async upgradegroupGetGroupList(){
            let res = await this.API.upgradegroupGetGroupList()
            this.groupListData = res
        },
        async upgradegroupwifiBatchJoinGroup(){
            let res = await this.API.upgradegroupwifiBatchJoinGroup({
                upgradeGroupId:this.form.upgradeGroupId,
                inverterSns:this.selectDataList.map(item=>{return item.deviceSN}),
            })
            if(!res.success){
				 	if(res.msg == 2){
                        this.comm.showMsg(false, this.$t('device-add-max-limit'))
						return
					}
                    this.comm.showMsg(false,this.$t('index.control_fail'))
						
				}else{
					if(res.object&&res.object.exsitLists.length>0){
						if(res.object.exsitLists.length == 10){
                            this.comm.showMsg(false,this.$t('device-contained') + `:${[...res.object.exsitLists]}`)
						}else{
                            this.comm.showMsg(false,this.$t('device-contained') +`:${[...res.object.exsitLists]},`+ this.$t('device-other-join-success') )
						}
					}else{
						this.comm.showMsg(true,this.$t('device-join-success'))
					}
                    this.$store.commit('setVisibleAdd',false)
                    this.$emit('uploadData',true)
				}
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.upgradegroupwifiBatchJoinGroup()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-add{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
}
</style>
